<?php
$data_count = $db_source->query('SELECT COUNT(*) as ucount FROM '.$old_nav_db_config['DB_PREFIX'].'user WHERE status = 1');
if(count($data_count) > 0)
	$data_count = $data_count[0]['ucount'];
else
	$data_count = 0;
	
$is_complete = false; 

if($data_count > $begin)
{
	$limit = $data_count - $begin;
	
	if($limit > 1000)
		$limit = 1000;
	
	$data_list = $db_source->query('SELECT * FROM '.$old_nav_db_config['DB_PREFIX'].'user WHERE status = 1 LIMIT '.$begin.','.$limit);
	
	if(!empty($old_bbs_db_config['DB_HOST']))
		$db_bbs = Db::getInstance(array('dbms'=>'mysql','hostname'=>$old_bbs_db_config['DB_HOST'],'username'=>$old_bbs_db_config['DB_USER'],'password'=>$old_bbs_db_config['DB_PWD'],'hostport'=>$old_bbs_db_config['DB_PORT'],'database'=>$old_bbs_db_config['DB_NAME']));
	
	include ROOT_PATH.'convert/Common/bbs.php';
	
	foreach($data_list as $data)
	{
		$user = array();
		$user['user_name'] = $data['user_name'];
		$user['user_pwd'] = $data['user_pwd'];
		$user['email'] = $data['email'];
		$is_bbs_usuer = false;
	
		if(!empty($old_bbs_db_config['DB_HOST']))
		{
			$bbs_user = $db_bbs->query('SELECT * FROM '.$old_bbs_db_config['DB_PREFIX']."members WHERE username = '".$data['user_name']."'");
			if(count($bbs_user) > 0)
			{
				$bbs_user = $bbs_user[0];
				$is_bbs_usuer = true;
				
				if($bbs_user['adminid'] == 1 && $bbs_user['groupid'] == 1)
					continue;
		
				$user['credits'] = $bbs_user['credits'];
				$user['extcredits1'] = $bbs_user['extcredits1'];
				$user['extcredits2'] = $bbs_user['extcredits2'];
				$user['extcredits3'] = $bbs_user['extcredits3'];
				$user['extcredits4'] = $bbs_user['extcredits4'];
				$user['extcredits5'] = $bbs_user['extcredits5'];
				$user['extcredits6'] = $bbs_user['extcredits6'];
				$user['extcredits7'] = $bbs_user['extcredits7'];
				$user['extcredits8'] = $bbs_user['extcredits8'];
				$user['regip'] = $bbs_user['regip'];
				$user['regdate'] = $bbs_user['regdate'];
				$user['lastip'] = $bbs_user['lastip'];
				$user['lastvisit'] = $bbs_user['lastvisit'];
				$user['lastactivity'] = $bbs_user['lastactivity'];
			}
		}
		
		if(!$is_bbs_usuer)
		{
			$user['credits'] = $data['score'];
			$user['extcredits1'] = 0;
			$user['extcredits2'] = $data['score'];
			$user['extcredits3'] = 0;
			$user['extcredits4'] = 0;
			$user['extcredits5'] = 0;
			$user['extcredits6'] = 0;
			$user['extcredits7'] = 0;
			$user['extcredits8'] = 0;
			$user['regip'] = $data['last_ip'];
			$user['regdate'] = $data['create_time'];
			$user['lastip'] = $data['last_ip'];
			$user['lastvisit'] = $data['last_time'];
			$user['lastactivity'] = $data['last_time'];
		}
	
		$uid = BBSAction::getInstance()->createUser($user);
		$salt = substr(uniqid(rand()), -6);
		$password = md5($data['user_pwd'].$salt);
		$db_target->query('UPDATE '.$db_config['DB_PREFIX']."ucenter_members SET password = '$password', salt = '$salt' WHERE username = '".$data['user_name']."'");
		$db_target->query('UPDATE '.$old_nav_db_config['DB_PREFIX']."goods_bond SET user_id = '$uid' WHERE user_id = ".$data['id']);
		$db_target->query('UPDATE '.$old_nav_db_config['DB_PREFIX']."message SET user_id = '$uid' WHERE user_id = ".$data['id']);
		$db_target->query('UPDATE '.$old_nav_db_config['DB_PREFIX']."order SET user_id = '$uid' WHERE user_id = ".$data['id']);
		$db_target->query('UPDATE '.$old_nav_db_config['DB_PREFIX']."site SET user_id = '$uid' WHERE user_id = ".$data['id']);
		showjsmessage("转换会员　$data[user_name] ... 成功");
	}
	
	if($limit < 1000)
		$is_complete = true; 
	else
		showjsmessage(U('Index/convert',array('table'=>'user','begin'=>$begin + $limit)),5);
}
else
	$is_complete = true;

if($is_complete)
{
	@unlink(ROOT_PATH.'public/convert_temp.php');
	@file_put_contents($this->install_lock,"");
	@file_put_contents($this->convert_lock,"");
	@mkdir(ROOT_PATH."admin/Runtime/",0777);
	showjsmessage("转换成功",4);
}
?>